package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.entity.Room;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.mapper.RoomMapper;
import com.zzyl.mapper.RoomTypeMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private RoomTypeMapper roomTypeMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    //创建床位
    @Override
    public void createBed(BedDto bedDto) {
        //判断床位编号不能重复
        Bed count = bedMapper.countDiffBed(bedDto.getBedNumber());
        if (count != null) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_REPEAT_BED_MUMBER);
        }

        Bed bed = new Bed();

        BeanUtils.copyProperties(bedDto, bed);
        bed.setBedStatus(0);
        bedMapper.addBed(bed);

        //修改房型对应的床位数
        //1.根据roomId查询床位对应的房间信息
        Room room = roomMapper.selectById(bedDto.getRoomId());
        if (room != null) {
            //根据房型名称，修改对应房型的床位数
            roomTypeMapper.updateBedNumber(room.getTypeName());
        }
    }

    //获取单个床位信息
    @Override
    public BedVo readBedById(Integer id) {
        Bed bed = bedMapper.findBedById(id);
        //不用new 对象了
//        BedVo bedVo = new BedVo();
//        BeanUtils.copyProperties(bed, bedVo);
        BedVo bedVo = BeanUtil.copyProperties(bed, BedVo.class);
        return bedVo;

    }

    @Override
    public void updateBed(BedDto bedDto) {
        //判断状态必须是0才能修改，此处需要在数据库查询，因为前端没有传递信息
        Bed bedById = bedMapper.getBedById(bedDto.getId());
        if (bedById.getBedStatus() != 0) {
            throw new BaseException(BasicEnum.BED_EDIT_FAIL_INUSE);
        }

        //判断床位编号不能重复
        Long count = bedMapper.countDiffBedNumber(bedDto.getBedNumber(), bedDto.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_REPEAT_BED_MUMBER);
        }

//        Bed bed = new Bed();
//        BeanUtils.copyProperties(bedDto, bed);
        Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);
        bedMapper.fixBed(bed);

    }

    @Override
    public void deleteBed(Long id) {
        //根据床位id查询对应的床位
        Bed bed = bedMapper.getBedById(id);
        if (bed != null) {
            bedMapper.deleteBedById(id);

            //修改对应房型的床位数
            Room room = roomMapper.selectById(bed.getRoomId());
            if (room != null) {
                roomTypeMapper.decrBedCountByRoomTypeName(room.getTypeName());
            }

        }

    }
}

